package com.yiren.manager.ctrl.personnel;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.security.MD5Hash;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrAccount;
import com.yiren.entity.YrAccountModelConfig;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrStaffInfoForm;
import com.yiren.manager.form.personnel.YrAccountForm;
import com.yiren.manager.form.system.YrAccountModelConfigForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.personnel.YrAccountService;
import com.yiren.manager.service.iface.system.YrAccountModelConfigService;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("account")
@Controller
public class YrAccountController extends GenericController<YrAccountForm,YrAccount>{
	
	@Autowired
	private YrAccountService selfService;
	
	@Autowired
	private YrStaffInfoService yrStaffInfoService;
	
	@Autowired
	private YrAccountModelConfigService accountModelConfigService;
	
	@RequestMapping("/register")
	@ResponseBody
	public HTTPResult<YrAccountForm> register(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrAccountForm> result = new HTTPResult<YrAccountForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrAccountForm form = tool.mapper("account", YrAccountForm.class);
		if(null==form){
			throwExp("param.error");
		}
		form.setId(UUIDGenerator.getUUID());
		form.setCreateTime(new Date());
		form.setUserLevel(2);//设置为普通用户 1是超级管理员 2是普通用户
		
		form.setPassword(MD5Hash.encryptPwd(form.getPassword()));
		YrStaffInfo staffInfo = yrStaffInfoService.findOne(form.getStaffId());
		if(staffInfo.getAccountStatus()==1){
			throwExp("该职工账号已开通，请勿重复开通");
		}
		if(null!=staffInfo && staffInfo.getAccountStatus()==0){
			staffInfo.setAccountStatus(1);
			yrStaffInfoService.save(staffInfo);
		}
		form.setUpdateTime(new Date());
		YrAccount entity = ObjectUtils.objectToObject(form, YrAccount.class);
		selfService.save(entity);
		
		return result;
	}
	
	/**
	 * 更新密码
	 * @param accountId
	 * @param passWord
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws InvocationTargetException
	 * @throws IntrospectionException
	 */
	@RequestMapping("/updatePassword")
	@ResponseBody
	public HTTPResult<YrAccountForm> updatePassword(String accountId,String passWord,String oldPassWord) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrAccountForm> result = new HTTPResult<YrAccountForm>();
		
		if(null==accountId||null==passWord||null==oldPassWord){
			throwExp("param.error");
		}
		
		YrAccount entity = selfService.findOne(accountId);
		if(null==entity){
			throwExp("查询不到对应的账号信息");
		}
		//如果输入的旧密码有误
		if(!MD5Hash.encryptPwd(oldPassWord).equals(entity.getPassword())){
			throwExp("旧密码有误");
		}
		
		if(MD5Hash.encryptPwd(passWord).equals(MD5Hash.encryptPwd(entity.getPassword()))){
			throwExp("新密码不能与旧密码相同");
		}
		
		entity.setPassword(MD5Hash.encryptPwd(passWord));
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public HTTPResult<YrAccountForm> login(String data) throws Exception{
		HTTPResult<YrAccountForm> result = new HTTPResult<YrAccountForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrAccountForm form = tool.mapper("account", YrAccountForm.class);
		if(null==form){
			throwExp("param.error");
		}
		String sqlWhere = "account=?1 AND password=?2";
		QueryResult<YrAccount> qr = selfService.find(YrAccount.class, sqlWhere, new Object[]{form.getAccount(),MD5Hash.encryptPwd(form.getPassword())});
		if(qr.getTotalRecord()<1){
			throwExp("账号或密码有误");
		}
		YrAccountForm accountForm = toForm(qr.getResultList().get(0));
		
		YrStaffInfo staffInfo = yrStaffInfoService.findOne(accountForm.getStaffId());
		
		if(null==staffInfo){
			throwExp("该账号未有绑定员工");
		}
		
		accountForm.setStaffInfoForm(ObjectUtils.objectToObject(staffInfo, YrStaffInfoForm.class));
		
		sqlWhere = "account_id=?1";
		QueryResult<YrAccountModelConfig> qrAccountModelConfig = accountModelConfigService.find(YrAccountModelConfig.class, sqlWhere,new Object[]{accountForm.getId()});
		List<YrAccountModelConfigForm> accountModelConfigForms = new ArrayList<YrAccountModelConfigForm>();
		for(YrAccountModelConfig entity : qrAccountModelConfig.getResultList()){
			accountModelConfigForms.add(ObjectUtils.objectToObject(entity, YrAccountModelConfigForm.class));
		}
		
		accountForm.setAccountModelConfigForms(accountModelConfigForms);
		
		List<YrAccountForm> formList = new ArrayList<YrAccountForm>();
		formList.add(accountForm);
		result.setData(formList);
		
		return result;
	}
	
	
	@Override
	public YrAccountForm toForm(YrAccount e) throws Exception {
		YrAccountForm form = ObjectUtils.objectToObject(e, YrAccountForm.class);
		return form;
	}

	@Override
	public YrAccount toEntity(YrAccountForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
